//
//  XHFollowSegmentControl.m
//  ShowYork
//
//  Created by hxh on 2020/10/13.
//  Copyright © 2020 hxh. All rights reserved.
//

typedef void(^doneBlock)(NSInteger index);

#import "XHFollowSegmentControl.h"

@interface XHFollowSegmentControl ()
@property (nonatomic,copy)NSArray <NSString *>*titleArray;
@property (nonatomic,strong)UIView *line;
@property (nonatomic,strong)UIImageView *bottomView;
@property (nonatomic,strong)UIButton *lastBtn;
@property (nonatomic,copy)doneBlock done;
@property (nonatomic,strong)NSMutableArray *buttons;
@end

@implementation XHFollowSegmentControl

- (NSMutableArray *)buttons{
    if (!_buttons) {
        _buttons = [NSMutableArray array];
    }
    return _buttons;
}

- (instancetype)initWithSourceTitles:(NSArray<NSString *> *)titles completeBlock:(void (^)(NSInteger))completeBlock{

    if (self = [super init]) {
        self.titleArray = titles;
        [self setUI];
        if (completeBlock) {
            self.done = ^(NSInteger index) {
                completeBlock(index);
            };
        }
    }
    return self;
}

- (void)setUI{
    
        
    self.frame = CGRectMake(0,0, kScreenW, 50);
    
    CGFloat maxWidth = kScreenW/self.titleArray.count;
    
    [self.titleArray enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton *btn = [[UIButton alloc]init];
        btn.tag = idx;
        [btn addTarget:self action:@selector(coverMethod:) forControlEvents:UIControlEventTouchUpInside];
        [btn setTitle:obj forState:UIControlStateNormal];
        [btn setTitleColor:C51 forState:UIControlStateSelected];
        [btn setTitleColor:C153 forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont boldSystemFontOfSize:18];
        [self addSubview:btn];
        
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(self);
            make.width.mas_equalTo(maxWidth);
            make.left.equalTo(self).offset(idx * maxWidth);
        }];
        
        if (idx == 0) {
            btn.selected = YES;
            self.lastBtn = btn;
        }
        
        [self.buttons addObject:btn];
    }];
    
    self.line = [[UIView alloc]init];
    self.line.backgroundColor = RGBA(230 ,230, 230, 1);
    [self addSubview:self.line];
    
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.equalTo(self);
        make.height.mas_equalTo(0.5);
    }];
    
    self.bottomView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"mine_bottomLine_tag"]];
    [self addSubview:self.bottomView];
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(2);
        make.bottom.equalTo(self);
        make.centerX.equalTo(self.lastBtn);
    }];
    
}


- (void)coverMethod:(UIButton *)btn{
    
    if (self.lastBtn == btn) {
        return;
    }
    
    self.lastBtn.selected = NO;
    
    btn.selected = YES;
    
    self.lastBtn = btn;
    
    [UIView animateWithDuration:0.25 animations:^{
        [self.bottomView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(2);
            make.bottom.equalTo(self);
            make.centerX.equalTo(btn);
        }];
        [self layoutIfNeeded];
    }];
    
    if (self.done) {
        self.done(btn.tag);
    }
    
}

- (void)showInView:(UIView *)view{
    [view addSubview:self];
}

- (void)setIndex:(NSInteger)index{
    _index = index;
    UIButton *btn = self.buttons[index];
    self.lastBtn.selected = NO;
    btn.selected = YES;
    self.lastBtn = btn;
    [self layoutIfNeeded];
    [UIView animateWithDuration:0.25 animations:^{
        [self.bottomView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(2);
            make.bottom.equalTo(self);
            make.centerX.equalTo(btn);
        }];
        [self layoutIfNeeded];
    }];
}
- (void)setFansCount:(NSNumber *)fansCount{
    
    _fansCount = fansCount;
    
    UIButton *fansBtn = self.buttons.lastObject;
    if (fansCount.intValue == 0) {
        [fansBtn setTitle:@"粉丝 0" forState:UIControlStateNormal];
    }else if (fansCount.intValue >= 10000) {
        NSInteger count = fansCount.intValue;
        if (count % 1000 == 0) {
            count += 1;
        }
        [fansBtn setTitle:[NSString stringWithFormat:@"粉丝 %@w",[RPAppManager notRounding:count/10000.0f afterPoint:1]] forState:UIControlStateNormal];
    }else{
        [fansBtn setTitle:[NSString stringWithFormat:@"粉丝 %@",fansCount] forState:UIControlStateNormal];
    }
}
- (void)setFollowCount:(NSNumber *)followCount{
    
    _followCount = followCount;
    
    UIButton *followBtn = self.buttons.firstObject;
    if (followCount.intValue == 0) {
        [followBtn setTitle:@"关注 0" forState:UIControlStateNormal];
    }else if (followCount.intValue >= 10000) {
        
        NSInteger count = followCount.intValue;
        if (count % 1000 == 0) {
            count += 1;
        }
        [followBtn setTitle:[NSString stringWithFormat:@"关注 %@w",[RPAppManager notRounding:count/10000.0f afterPoint:1]] forState:UIControlStateNormal];
    }else{
        [followBtn setTitle:[NSString stringWithFormat:@"关注 %@",followCount] forState:UIControlStateNormal];
    }
}



@end
